﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenGado.DATA;
using OpenGado.DATA.Entities;

namespace OpenGado.Win
{
    public partial class frmRequests : Form
    {
        private enum SearchMode 
        {
            On, Off
        }

        private SearchMode currentMode; 

        public frmRequests()
        {
            currentMode = SearchMode.Off;

            InitializeComponent();
            GetRequestList(GenRequestList("All"));
        }

        private IList<IssueItemHeader> GenRequestList(String searchCriteria)
        {
            //check if search box is empty 
  IList<IssueItemHeader> headerList = null;
            
            switch (searchCriteria)
            {
                  case "Date":
                    {
                        headerList =  IssueItemsDataControl.Instance.GetListOfHeaders(dtpStartDate.Value, dtpEndDate.Value);                   
                        break;
                    }
                case "Project":
                    {
                        headerList = IssueItemsDataControl.Instance.GetListOfHeaders("Project", txtSearchValue.Text);
                        break;
                    }
                case "Request Number":
                    {
                        headerList = IssueItemsDataControl.Instance.GetListOfHeaders("RequestNumber",txtSearchValue.Text);
                        break;
                    }
                case "Status":
                    {
                       
                        headerList = IssueItemsDataControl.Instance.GetListOfHeaders("RequestStatus", txtSearchValue.Text);
                        break;
                    }
                default:
                    {
                         headerList = IssueItemsDataControl.Instance.GetListOfHeaders();
                        break;
                    }

            }

            return headerList;
        }

        private void GetRequestList(IList<IssueItemHeader> headerList)
        {
          
            //dgvRequests.Rows.Clear();

            dgvRequests.AutoGenerateColumns = false;
            dgvRequests.DataSource = headerList;

          
            // bind the columns manually. 
            dgvRequests.Columns[0].DataPropertyName = "IssueItemHeaderId";
            dgvRequests.Columns[1].DataPropertyName = "RequestNumber";
            dgvRequests.Columns[2].DataPropertyName = "RequestDate";
            dgvRequests.Columns[3].DataPropertyName = "RequestBy";
            dgvRequests.Columns[4].DataPropertyName = "RequestStatus"; 
            // display the data
        }

     

        private void btnNew_Click(object sender, EventArgs e)
        {

            frmItemIssue issueForm = new frmItemIssue();
            issueForm.modeType = frmItemIssue.CurrModeType.request;

            issueForm.Show();

         
        }

        private void frmRequests_Load(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // searches
        private void btnSearch_Click(object sender, EventArgs e)
        {
            switch (cboSearchBy.Text)
            {
                case "Date":
                    {
                        GetRequestList(GenRequestList("Date")); 
                        break;
                    }
                case "Project":
                    {
                        GetRequestList(GenRequestList("Project")); 
                        break;
                    }
                case "Request Number":
                    {

                        GetRequestList(GenRequestList("Request Number")); 
                        break;
                    }
                case "Status":
                    {

                        GetRequestList(GenRequestList("Status"));
                        break;
                    }


            }
        }

        private void cboSearchBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboSearchBy.Text =="Date")
            {
                gpDateSearch.Visible = true;
                txtSearchValue.Text = "";
                txtSearchValue.Visible = false;

            }
            else
            {
                gpDateSearch.Visible = false;
                txtSearchValue.Visible = true;
            }
        }

        private List<int> GetSelectedRows()
        {
            List<int> selectedRows = new List<int>();

                foreach (DataGridViewRow rw in dgvRequests.SelectedRows)
                {
                    int id;
                    int.TryParse(rw.Cells[0].Value.ToString(), out id);
                    selectedRows.Add(id);
                }

            return selectedRows;
        }
      
        private void DeleteSelection()
        {
            foreach (var i in GetSelectedRows())
            {
                IssueItemHeader itemHeader = new IssueItemHeader();
                itemHeader.IssueItemHeaderId = i;

                IssueItemsDataControl.Instance.DeleteHeader(itemHeader);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Are you sure you want to delete the selection", "Inventory", MessageBoxButtons.YesNo); 
            if (res == DialogResult.Yes)
            {
                    DeleteSelection();
                    GetRequestList(GenRequestList("All"));
            }
        }

        private void btnIssue_Click(object sender, EventArgs e)
        {
            if (dgvRequests.CurrentRow != null)
            {
                int selectedRow = (int)dgvRequests.CurrentRow.Cells[0].Value;
                frmItemIssue issueform = new frmItemIssue(selectedRow, frmItemIssue.CurrModeType.issue);
                issueform.MdiParent = this.MdiParent;
                issueform.Show();


            }
        }

        private void dgvRequests_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvRequests.CurrentRow != null)
            {
                int selectedRow = (int)dgvRequests.CurrentRow.Cells[0].Value;
                frmItemIssue issueform = new frmItemIssue(selectedRow, frmItemIssue.CurrModeType.request);
                issueform.MdiParent = this.MdiParent;
               issueform.Show();


            }

        }
    }
}
